<?php

/**
 * @version    $Id: whmcsauth.php 7180 2008-10-29 12:00:00 M.Bridger $
 * @package    joomla_katolika
 * @subpackage Plugins
 * @license    GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.event.plugin');


/**
 * External BloggerAPI Database Authentication Plugin.  Based on the example in Joomla! Core
 *
 * @package    joomla_katolika
 * @subpackage Plugins
 * @license    GNU/GPL
 */

class plgAuthenticationBloggerAPI_auth extends JPlugin
{
	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param	object	$subject	The object to observe
	 * @param	array	$config		An array that holds the plugin configuration
	 * @since	1.5
	 */

	function plgAuthenticationBloggerAPI_auth(& $subject, $config){
		parent::__construct($subject, $config);

	}
	/**
	 * This method should handle any authentication and report back to the subject
	 *
	 * @access	public
	 * @param	array	$credentials	Array holding the user credentials
	 * @param	array	$options		Array of extra options
	 * @param	object	$response		Authentication response object
	 * @return	boolean
	 * @since	1.5
	 */

	function onAuthenticate( $credentials, $options, &$response )
	{
		/*
		* Here you would do whatever you need for an authentication routine with the credentials
		*
		* In this example the mixed variable $return would be set to false
		* if the authentication routine fails or an integer userid of the authenticated
		* user if the routine passes
		*/
		$plugin =&JPluginHelper::getPlugin('authentication','bloggerAPI_auth');

		$params = new JParameter( $plugin->params );
		$host    		= $params->get('bloggerapi_host');			
		$path    		= $params->get('bloggerapi_path');			
		$appkey    		= $params->get('bloggerapi_appkey');			

		// Includes the required class file for the XML-RPC Client
		jimport('phpxmlrpc.xmlrpc');
		//debugging
		$client = new xmlrpc_client($path, $host, 80);
		
		$msg = new xmlrpcmsg('blogger.getUserInfo', array(new xmlrpcval($appkey, "string"), new xmlrpcval($credentials["username"], "string"), new xmlrpcval($credentials["password"], "string")));
		
		$xmlrpcdoc = $client->send($msg);		

		if ($xmlrpcdoc->faultCode()== 0)
		{
			$scalar_var = $xmlrpcdoc->value();
			foreach ($scalar_var->scalarval() as $key => $val):
			$results[$key] = $val->scalarval();
			endforeach;

			
			$response = JUser::getInstance(); // Bring the user in line with the rest of the system
			if ($results["firstname"] || $results["lastname"])
			{
				$response->fullname =  $results["firstname"]." ".$results["lastname"];
			}
			else
			{
				$response->fullname = $credentials["username"];
			}
			$response->email = $results["email"];
			$response->status = JAUTHENTICATE_STATUS_SUCCESS;
			return true;
		}
		else
		{
		
		
			$response->status = JAUTHENTICATE_STATUS_FAILURE;
			$response->error_message = $xmlrpcdoc->faultString();
			return false;			
		}
				
	}

}

?> 